﻿//页面加载
$(document).ready(function () {
    $("#btnReset").bind("click", btnReset_click);
    $("#btnSearch").bind("click", BindDataList);
    $("#btnCreate").bind("click", btnCreate_click);
    $("#btnDeleteAll").bind("click", btnDeleteAll_click);
    $("#checkall").bind("click", CheckAll_click);
    BindDataList();
})
//绑定数据
function BindDataList() {
    var name = $.trim($("#txtCode").val());
    var parameters = MakeParameters("VipCardController", "GetPageList", escape(name));
    var page = new Pagger(parameters, BindDataToTable);
    PageIndexChanging(0);
}
function BindDataToTable(result) {
    if (Pager.RecountCount == -1) {
        $("#errorDataTD").show();
        return;
    }
    else {
        $("#errorDataTD").hide();
    }
    var iCount = 0;
    var $table = $("#TableList");
    var $template = $("#template");
    $table.find(".contentRow").remove();
    $.each(result, function (i, n) {
        iCount++;
        var row = $template.clone();
        row.css("display", "");
        //数据项
        row.find("#td_checkbox").html("<input type='checkbox' name='checkbox'  class='chk' value='" + n.AID + "'/>");
        row.find("#td_Code").html("<a href='/VipCard/Show?action=Details&ID=" + n.AID + "'>" + n.Code + "</a>");
        //row.find("#td_MatchRule").html("<a href='javascript:void(0)' title='计算该频道的用户数量' style='text-align:center' id=tduser" + n.ID + " onclick=AccountCurrentUserNumber(" + n.ID + ",'" + n.Keyword + "')>计算用户数量</a>");
        row.find("#td_RealName").html(n.RealName);
        row.find("#td_Phone").html(n.Phone);
        row.find("#td_CardType").html(n.CardType); 
        row.find("#td_CardLevelName").html(n.CardLevelName);
        if (n.CardType == "计次卡") {
            row.find("#td_Balance").html(n.Balance + " 次");
            row.find("#td_Total").html(n.Total + " 次");
        }
        else {
            row.find("#td_Balance").html(n.Balance + " 元");
            row.find("#td_Total").html(n.Total + " 元");
        }

        row.find("#td_Recharge").html(n.Recharge+ " 元");
        //var v_endDate = CutString(n.EndDate, 0, 10)
        //if (v_endDate == "2999-01-01") {
        //    v_endDate = "永久";
        //}
        row.find("#td_EndDate").html(n.EndDateString);
        row.find("#td_State").html(n.State);
        //操作
        row.find("#btnEdit").bind("click", { ID: n.AID }, btnEdit_click);
        row.find("#btnDelete").bind("click", { ID: n.AID }, btnDelete_click);
        row.find("#btnResetPwd").bind("click", { ID: n.Code }, btnResetPwd_click); 
        row.find("#btnRecharge").bind("click", { ID: n.Code }, btnRecharge_click);
        
        if (n.CardType == "计次卡") {
            row.find("#td_Balance").html(n.Balance+" 次");
            //计次卡不能充值
            row.find("#btnRecharge").attr("class", "dis_n");           
            if (n.EndDateString == "过期") {
                row.attr("class", "contentRow t_c cf60"); 
            }
            else {
                row.attr("class", "contentRow t_c green");
            }
        }
        else {
            row.find("#td_Balance").html(n.Balance + " 元");
            row.attr("class", "contentRow t_c");
        }
        row.appendTo($table);
    });
    if (iCount == 0) {
        $("#emptyDataTD").show();
    }
    else {
        $("#emptyDataTD").hide();
    }
}
//全选
function CheckAll_click() {
    var chk = $(this);
    chk.parent().parent().parent().next().children().find("input:checkbox").attr("checked", chk.prop("checked"));
}
//重置界面
function btnReset_click() {
    $("#txtUserName").val("");
}

//设置菜单权限
function btnResetPwd_click(e) {
    var v_code = e.data.ID;
    if (confirm("你确定将卡号：[ "+v_code+" ]的密码重置为888888？")) {
        $.post("/VipCard/ResetPassword", { code: v_code }, function (result) {
            if (result.code == "OK") {
                alert("密码重置成功")
            }
            else {
                alert("操作失败，请稍后重试");
            }
        });
    }
}

//编辑
function btnRecharge_click(e) {
    var Id = e.data.ID;
    window.location.href = "/VipCard/Recharge?code=" + Id;
}

//创建
function btnCreate_click() {
    window.location.href = "/VipCard/Create";
}

//编辑
function btnEdit_click(e) {
    var Id = e.data.ID;
    window.location.href = "/VipCard/Show?Action=Update&ID=" + Id;
}

//批量删除
function btnDeleteAll_click() {
    var strIDs = "";
    $(this).parent().parent().parent().next().children().find(".chk").each(function (i, item) {
        if ($(item).prop("checked")) {
            //字符串ID
            //strIDs += "\""+$(item).val() + "\";"; 
            //整形ID
            strIDs += $(item).val() + ",";
        }
    });
    if (strIDs == "") {
        alert("请至少选择一条数据!");
        return;
    }
    if (confirm("你确认要删除该数据?")) {
        $.post("/VipCard/Delete", { IDs: strIDs }, function (result) {
            if (result.code == "OK") {
                alert("删除成功!");
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert(result.msg);
            }
        },"json");
    }
}
//删除单项
function btnDelete_click(e) {
    var Id = e.data.ID;
    if (confirm("确认要删除该数据吗?")) {
        $.post("/VipCard/Delete", { IDs: Id }, function (result) {
            if (result.code == "OK") {
                alert("删除成功!");
                PageIndexChanging(Pager.CurrentPage);
            }
            else {
                alert(result.msg);
            }
        },"json");
    }
}
//时间字符串截取
function CutString(str, index, length) {
    if (str != null && str != undefined) {
        if (str.length >= length) {
            if (length > 10) {
                var str1 = str.substring(index, length);
                return str1.replace("T", " ");
            }
            else {
                return str.substring(index, length);
            }
        }
        else {
            return str;
        }
    }
    else {
        return "";
    }
}
